package com.huike.report.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.page.TableDataInfo;
import com.huike.contract.domain.TbContract;
import com.huike.contract.domain.vo.CountActivityId;
import com.huike.contract.domain.vo.GetAmountWithCost;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.Index;
import com.huike.report.domain.dto.IndexDto;
import com.huike.report.domain.vo.*;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class ReportServiceImpl implements IReportService {
    @Autowired
    private TbClueMapper clueMapper;
    @Autowired
    private TbBusinessMapper businessMapper;
    @Autowired
    private TbContractMapper contractMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private TbContractMapper tbContractMapper;



    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * *************看我看我**************
     * 传入两个时间范围，返回这两个时间范围内的所有时间，并保存在一个集合中
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<String> findDates(String beginTime, String endTime)
            throws ParseException {
        List<String> allDate = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        allDate.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            allDate.add(sdf.format(calBegin.getTime()));
        }
        System.out.println("时间==" + allDate);
        return allDate;
    }


    /**
     * ************看我看我***********
     * 用我能少走很多路
     * 我是用来机选百分比的方法
     *
     * @param all
     * @param num
     * @return
     */
    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }

    @Override
    public IndexVo index(IndexDto indexDto) {
        Index index = new Index();
        Index indexToday = new Index();
        indexToday.setTime(LocalDateTime.now().toString());

        BeanUtils.copyProperties(indexDto, index);

        Integer cluesNum = reportMapper.countClues(index);
        Integer todayCluesNum = reportMapper.countClues(indexToday);

        Integer businessNum = reportMapper.countBusiness(index);
        Integer todayBusinessNum = reportMapper.countBusiness(indexToday);

        Integer contractNum = reportMapper.countContractByTime(index);
        Integer todayContractNum = reportMapper.countContractByTime(indexToday);

        Double salesAmount = reportMapper.sumBusinessByTime(index);
        salesAmount = salesAmount == null ? 0.0 : salesAmount;
        Double todaySalesAmount = reportMapper.sumBusinessByTime(indexToday);
        todaySalesAmount = todaySalesAmount == null ? 0.0 : todaySalesAmount;

        index.setStatus(1);
        Integer tofollowedCluesNum = reportMapper.countClues(index);
        Integer tofollowedBusinessNum = reportMapper.countBusiness(index);

        index.setType(0);
        Integer locatedCluesNum = reportMapper.countClueOrBusinessByAssign(index);
        Integer toallocatedCluesNum = cluesNum - locatedCluesNum;

        index.setType(1);
        Integer locatedBusinessNum = reportMapper.countClueOrBusinessByAssign(index);
        Integer toallocatedBusinessNum = businessNum - locatedBusinessNum;

        IndexVo indexVo = new IndexVo();
        indexVo.setBusinessNum(businessNum);
        indexVo.setCluesNum(cluesNum);
        indexVo.setTodayCluesNum(todayCluesNum);
        indexVo.setTodayBusinessNum(todayBusinessNum);
        indexVo.setContractNum(contractNum);
        indexVo.setTodayContractNum(todayContractNum);
        indexVo.setSalesAmount(salesAmount);
        indexVo.setTodaySalesAmount(todaySalesAmount);
        indexVo.setTofollowedCluesNum(tofollowedCluesNum);
        indexVo.setTofollowedBusinessNum(tofollowedBusinessNum);
        indexVo.setToallocatedCluesNum(toallocatedCluesNum);
        indexVo.setToallocatedBusinessNum(toallocatedBusinessNum);


        return indexVo;
    }

    @Override
    public List<IndexChangeVo> indexClues(IndexDto indexDto) {
        List<String> userIds = reportMapper.selectUserIdOfClue();
        List<IndexChangeVo> list = new ArrayList<>();
        if(userIds == null || userIds.size() == 0){
            return null;
        }
        for (String userId : userIds) {
            IndexChangeVo indexChangeVo = new IndexChangeVo();

            String userName = reportMapper.selectNameByUserId(userId);
            Integer deptId = reportMapper.selectDeptId(userId);
            String deptName = reportMapper.selectDeptName(deptId);
            Integer num = reportMapper.countCluesByUserId(userId);
            Integer allClues = reportMapper.countClues(null);
            Double radio = 1.0 * num * 100 / allClues;

            indexChangeVo.setDep_id(deptId);
            indexChangeVo.setDeptName(deptName);
            indexChangeVo.setCreate_by(userName);
            indexChangeVo.setNum(num);
            indexChangeVo.setRadio(radio);

            list.add(indexChangeVo);
        }

        return list;
    }

    @Override
    public List<IndexChangeVo> indexBusiness(IndexDto indexDto) {
        List<String> usersId = reportMapper.selectUserIdOfBusiness();
        List<IndexChangeVo> list = new ArrayList<>();
        if(usersId == null || usersId.size() == 0){
            return null;
        }
        for (String userId : usersId) {
            IndexChangeVo indexChangeVo = new IndexChangeVo();

            String userName = reportMapper.selectNameByUserId(userId);
            Integer deptId = reportMapper.selectDeptId(userId);
            String deptName = reportMapper.selectDeptName(deptId);
            Integer num = reportMapper.countBusinessByUserId(userId);
            Integer allClues = reportMapper.countBusiness(null);
            Double radio = 1.0 * num * 100 / allClues;

            indexChangeVo.setDep_id(deptId);
            indexChangeVo.setDeptName(deptName);
            indexChangeVo.setCreate_by(userName);
            indexChangeVo.setNum(num);
            indexChangeVo.setRadio(radio);

            list.add(indexChangeVo);
        }

        return list;
    }

    /**
     * 漏斗图接口
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public VulnerabilityMapVo getVulnerabilityMap(String beginCreateTime, String endCreateTime) {
        /*VulnerabilityMapVo mapVo = new VulnerabilityMapVo();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.parse(beginCreateTime, formatter), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.parse(endCreateTime, formatter), LocalTime.MAX);
        //  线索数
        int cluesNums = clueMapper.getCluesNums(beginTime.toString(), endTime.toString());
        System.err.println("线索总数" + cluesNums);
        // 有效线索数
        int effectiveCluesNums = clueMapper.getEffectiveCluesNums(beginTime, endTime);

        System.err.println("线索总有效线索数" + effectiveCluesNums);

        // 商机数
        int businessNums = businessMapper.getBusinessNums(beginTime.toString(), endTime.toString());
        System.err.println("有效线索数" + businessNums);

        // 合同数
        int contracyNums = contractMapper.getContracyNums(beginTime, endTime);
        mapVo.setCluesNums(cluesNums);
        mapVo.setEffectiveCluesNums(effectiveCluesNums);
        mapVo.setBusinessNums(businessNums);
        mapVo.setContractNums(contracyNums);*/

        TbClue tbClue = new TbClue();
        List<TbClue> tbClues = clueMapper.selectTbClue(beginCreateTime, endCreateTime);
        Integer cluesNums = 0;
        Integer effectiveCluesNums = 0;
        Integer businessNums = 0;
        Integer contractNums = 0;

        for (TbClue clue : tbClues) {
            String statusClue = clue.getStatus();

            if(statusClue.equals("6")){
                String status = businessMapper.selectBusinessStatusByName(clue.getName());
                if(status.equals("7")){
                    contractNums++;
                }else if(status.equals("1") || status.equals("2")){
                    businessNums++;
                }
            }else if (statusClue.equals("1") || statusClue.equals("2") || statusClue.equals("6")){
                effectiveCluesNums++;
            }
            cluesNums++;
        }

        VulnerabilityMapVo mapVo = new VulnerabilityMapVo(cluesNums, effectiveCluesNums, businessNums, contractNums);

        return mapVo;
    }
    /**
     * 销售统计报表
     *
     * @param beginCreateTime
     * @param endTime
     * @return
     */
    @Override
    public LineChartVO saleStatistics(String beginCreateTime, String endTime) throws ParseException {
        //获取日期列表
        List<String> dateList = findDates(beginCreateTime, endTime);
        //获取series对象,销售额对象
        List<LineSeriesVO> series = new ArrayList<>();
        ArrayList<Object> data = new ArrayList<>();
        //封装y轴数据对象
        LineSeriesVO lineSeriesVO = new LineSeriesVO();
        lineSeriesVO.setName("销售统计");

        for (String date : dateList) {
            LocalDate localDate = LocalDate.parse(date);
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);

            Map map = new HashMap();
            map.put("beginTime", beginTime);
            map.put("end", end);

            //合同状态为4时才算销售成功
            Double seriesCount = tbContractMapper.saleStatistics(map);
            seriesCount = seriesCount == null ? 0.0 : seriesCount;
            data.add(seriesCount);

        }
        lineSeriesVO.setData(data);
        series.add(lineSeriesVO);
        //新建LineChartVO对象封装数据
        LineChartVO lineChartVO = new LineChartVO();
        //将日期封装到x轴
        lineChartVO.setxAxis(dateList);
        //将营业额数据封装到y轴
        lineChartVO.setSeries(series);
        return lineChartVO;
    }

    @Override
    public LineChartVO NewReport(String beginCreateTime,String endCreateTime) throws ParseException {

        List<String> dates = findDates(beginCreateTime,endCreateTime);
        LineChartVO lineChartVO = new LineChartVO();
        ArrayList<LineSeriesVO> lineSeriesVOs = new ArrayList<>();
        ArrayList<String> xAxis = new ArrayList<>();
        LineSeriesVO lineSeriesVO = new LineSeriesVO();
        LineSeriesVO lineSeriesVO1 = new LineSeriesVO();


        if (dates==null || dates.size()==0) {
            return null;
        }
        ArrayList<Object> data = new ArrayList<>();
        for (String date : dates) {
            LocalDate parse = LocalDate.parse(date);
            LocalDateTime begin =LocalDateTime.of(parse, LocalTime.MAX);
            LocalDateTime end = LocalDateTime.of(parse, LocalTime.MIN);

            Integer NewDay =reportMapper.NewDaySelect(begin.toString(),end.toString());


            xAxis.add(date);

            data.add(NewDay);
        }
        lineSeriesVO.setName("新增线索数");
        lineSeriesVO.setData(data);
        lineChartVO.setxAxis(xAxis);
        lineSeriesVOs.add(lineSeriesVO);
        ArrayList<Object> data1 = new ArrayList<>();
        for (String date : dates) {
            Integer OrderDay =reportMapper.OrderDaySelect(date);
            data1.add(OrderDay);
        }
        lineSeriesVO1.setName("总数");
        lineSeriesVO1.setData(data1);
        lineSeriesVOs.add(lineSeriesVO1);
        lineChartVO.setSeries(lineSeriesVOs);
        return lineChartVO;
    }

    @Override
    public List<ChanelStatisticsVo> chanelStatistics(String beginCreateTime, String endCreateTime) {
        List<ChanelStatisticsVo> list = reportMapper.chanelStatistics(beginCreateTime,endCreateTime);
        for (ChanelStatisticsVo chanelStatisticsVo : list) {
            String channel = chanelStatisticsVo.getChannel();
            String label = sysDictDataMapper.selectDictLabel("clues_item", channel);
            if (channel == null || !channel.equals("0") && !channel.equals("1")){
                label = "其他";
            }

            chanelStatisticsVo.setChannel(label);
        }

        return list;
    }

    @Override
    public List<ActivityStatisticsListVo> activityStatistics(String beginCreateTime, String endCreateTime) {
        /*List<TbContract> list = tbContractMapper.getContract(beginCreateTime,endCreateTime);
        ArrayList<ActivityStatisticsListVo> activityStatisticsListVos = new ArrayList<>();
        for (TbContract tbContract : list) {
            ActivityStatisticsListVo statisticsListVo = new ActivityStatisticsListVo();
            Long activityId = tbContract.getActivityId();
            TbActivity activity = reportMapper.getByActivityId(activityId);
            if (activity == null){
                statisticsListVo.setActivity("其他");
            }else {
            String name = activity.getName();
            statisticsListVo.setActivity(name);
            }
            Integer num = tbContractMapper.getNumByActivityId(activityId);
            statisticsListVo.setNum(num);
            statisticsListVo.setActivityId(activityId);
            activityStatisticsListVos.add(statisticsListVo);
        }
        return activityStatisticsListVos;*/
        List<CountActivityId> list = tbContractMapper.getContract(beginCreateTime,endCreateTime);
        ArrayList<ActivityStatisticsListVo> activityStatisticsListVos = new ArrayList<>();
        for (CountActivityId activityId : list) {
            ActivityStatisticsListVo activityStatisticsListVo = new ActivityStatisticsListVo();
            Long activityId1 = activityId.getActivityId();
            TbActivity activity = reportMapper.getByActivityId(activityId1);
            activityStatisticsListVo.setActivity(activity.getName());
            activityStatisticsListVo.setActivityId(activityId1);
            activityStatisticsListVo.setNum(activityId.getNum());
            activityStatisticsListVos.add(activityStatisticsListVo);
        }
        return activityStatisticsListVos;
    }

    @Override
    public ActivityStatisticsPageVo activityStatisticsList(String beginCreateTime, String endCreateTime, String code, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        //返回数据
        ArrayList<ActivityStatisticsVo> activityStatisticsVos = new ArrayList<>();

        //创建对象
        TbActivity tbActivity = TbActivity.builder().code(code).name(name).status("2").build();

        List<TbActivity> activities = reportMapper.getActivityStatisticsList(tbActivity);
        Page<TbActivity> page = (Page<TbActivity>) activities;
        for (TbActivity activity : activities) {
            //创建 ActivityStatisticsVo 对象
            ActivityStatisticsVo activityStatisticsVo = new ActivityStatisticsVo();
            //设置活动表数据
            BeanUtils.copyProperties(activity,activityStatisticsVo);

            //创建tbClue对象
            TbClue clue = TbClue.builder().activityId(activity.getId()).activityName(activity.getName())
                    .channel(activity.getChannel()).build();
            //获取总线索
            Integer cluesNum = tbClueMapper.getCountCluesNum(clue);
            //获取伪线索
            clue.setStatus("4");
            Integer falseCluesNum = tbClueMapper.getFalseWithBusNum(clue);
            //获取商机转换
            clue.setStatus("6");
            Integer businessNum = tbClueMapper.getFalseWithBusNum(clue);

            //创建对象 查询合同表
            TbContract tbContract = new TbContract();
            tbContract.setActivityId(activity.getId());
            tbContract.setChannel(activity.getChannel());
            GetAmountWithCost count = tbContractMapper.getAmountWithCost(tbContract);
            //设置合同表数据
            if (count == null){
                activityStatisticsVo.setAmount(0.0);
                activityStatisticsVo.setCost(0.0);
            }else {
                activityStatisticsVo.setAmount(count.getAmount());
                activityStatisticsVo.setCost(count.getCost());
            }

            //设置线索表数据
            activityStatisticsVo.setCluesNum(cluesNum);
            activityStatisticsVo.setFalseCluesNum(falseCluesNum);
            activityStatisticsVo.setBusinessNum(businessNum);
            activityStatisticsVo.setCustomersNum(count.getCustomersNum());

            //设置商机表数据
            activityStatisticsVo.setCustomersNum(count.getCustomersNum());
            activityStatisticsVos.add(activityStatisticsVo);

        }
        ActivityStatisticsPageVo activityStatisticsPageVo = new ActivityStatisticsPageVo();
        activityStatisticsPageVo.setTotal((int) page.getTotal());
        activityStatisticsPageVo.setRows(activityStatisticsVos);
        return activityStatisticsPageVo;

    }


    @Override
    public TableDataInfo CountSelect(Integer pageNum,Integer pageSize,String beginCreateTime,String endCreateTime) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        PageHelper.startPage(pageNum, pageSize);
        Page<ClueVo> list = reportMapper.CountSelect(beginCreateTime,endCreateTime);
        /*List<ClueVo> list1 = new ArrayList<>();
        for (ClueVo clueVo : list.getResult()) {
            if (clueVo.getClueStatus().equals("6")) {
                String businessCreateTime=reportMapper.business(clueVo.getName());
                String businessStatus=reportMapper.business1(clueVo.getName());
                clueVo.setBusinessCreateTime(businessCreateTime);
                clueVo.setBusinessStatus(businessStatus);
            }
            list1.add(clueVo);
        }*/
        tableDataInfo.setTotal(list.getTotal());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setParams(null);
        return tableDataInfo;
    }


    /**
     * 销售统计报表 根据部门分页查询
     * @param pageNum
     * @param pageSize
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TableDataInfo deptStatisticsList(Integer pageNum, Integer pageSize, LocalDate begin, LocalDate end) {
        //定义起始索引和每页记录数
        PageHelper.startPage(pageNum, pageSize);
        //把localdate对象转换为localdatetime
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        //通过mapper获取数据
        Page<DeptContractVO> page = reportMapper.deptStatisticsList(beginTime, endTime);
        List<DeptContractVO> pageResult = page.getResult();
        for (DeptContractVO deptContractVO : pageResult) {
            deptContractVO.setDept_Id(deptContractVO.getDeptId());
            deptContractVO.setTotal_amount(deptContractVO.getTotalAmount());
        }
        //封装分页查询对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(pageResult);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
        tableDataInfo.setParams(null);
        return tableDataInfo;
    }

    /**
     * 销售统计报表 根据所属人进行分页查询
     * @param pageNum
     * @param pageSize
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TableDataInfo ownerShipStatisticsList(Integer pageNum, Integer pageSize, LocalDate begin, LocalDate end) {
        //定义起始索引和每页记录数
        PageHelper.startPage(pageNum, pageSize);
        //日期格式转换
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        //通过mapper获取数据
        Page<OwnerVO> owner = reportMapper.ownerShipStatisticsList(beginTime, endTime);
        List<OwnerVO> pageResult = owner.getResult();
        //对获取到的数据进行遍历
        for (OwnerVO ownerVO : pageResult) {
            //将数据封装到ownerVO中
            ownerVO.setCreate_by(ownerVO.getCreateBy());
            ownerVO.setTotal_amount(ownerVO.getTotalAmount());
        }
        //封装分页查询对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(pageResult);
        tableDataInfo.setTotal(owner.getTotal());
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
        tableDataInfo.setParams(null);
        return tableDataInfo;
    }

    /***
     * 客户统计时间列表
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public LineChartVO selectAllClient(String beginCreateTime, String endCreateTime) {
        //获取Vo对象
        LineChartVO lineChartVO = new LineChartVO();
        try {
            //通用findDates方法获得总共的天数
            List<String> allTimeList = findDates(beginCreateTime, endCreateTime);

            //将天数给X轴
            lineChartVO.setxAxis(allTimeList);
            ArrayList<LineSeriesVO> series = new ArrayList<>();

            //查询每一天的人数
            List<Map<String, Object>> statistics = contractMapper.contractStatistics(beginCreateTime, endCreateTime);
            //两折线(新增,总数
            //新增

            LineSeriesVO lineSeries1 = new LineSeriesVO();
            lineSeries1.setName("新增客户数");

            //总数

            LineSeriesVO lineSeries2 = new LineSeriesVO();
            lineSeries2.setName("客户总数");
            //获取用户数
            int sum = 0;
            for (String s : allTimeList) {
                Optional optional = statistics.stream().filter(d -> d.get("date").equals(s)).findFirst();
                if (optional.isPresent()) {
                    Map<String, Object> cuurentData = (Map<String, Object>) optional.get();
//添加到lineseries的data中
                    lineSeries1.getData().add(cuurentData.get("num"));

                    sum += Integer.parseInt(cuurentData.get("num").toString());

                } else {
                    //为0新增数就为0
                    lineSeries1.getData().add(0);
                }
                lineSeries2.getData().add(sum);
            }
            series.add(lineSeries1);
            series.add(lineSeries2);
            lineChartVO.setSeries(series);
        } catch (ParseException e) {
            // e.printStackTrace();
        }
        return lineChartVO;


    }

    /**
     * 客户分页查询
     *
     * @param contract
     * @return
     */
    @Override
    public List<TbContract> contractStatisticsList(TbContract contract) {
        List<TbContract> tbContractList = contractMapper.selectTbContractList(contract);
        return tbContractList;


    }

    /**
     * 客户学科分布图
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
   /* @Override
    public List subject(String beginCreateTime, String endCreateTime) {
//查询各个不同的学科的一周内的人数(分组查询)
        //查询各个学科和学科相对应的人数
        List<Map<String, Object>> data = contractMapper.subject(beginCreateTime, endCreateTime, 4);

        for (Map<String, Object> map : data) {
            //根据subject键获取相对应的值
            String   value = (String) map.get("subject");
            //查询字典数据表,根据字典类型  和课程编号,查询到相对应的课程名称
            String s = sysDictDataMapper.selectDictLabel("coure_ject", value);
            //为subject键赋值
            map.put("subject",s);
        }


        return data;
    }
*/
    //  增强for  for(元素的数据类型 变量名 : 数组或者集合)
    //涉及两张表，合同表和字典数据表
    @Override
    public List<Map<String, Object>> subject(String beginCreateTime, String endCreateTime) {
        //查询各个不同的学科的一周内的人数（分组查询）
        //查询各个学科和学科对应的人数
        List<Map<String, Object>> data = contractMapper.subject(beginCreateTime, endCreateTime,4);

        for (Map<String, Object> map : data) {
            //根据subject键获取相对应的值(课程编号)。
            String value = (String) map.get("subject");
            //查询字典数据表，根据字典类型（为course_subject）和value（课程编号）查询到相对应的课程名称
            String zhi = sysDictDataMapper.selectDictLabel("course_subject", value);
            //为subject键赋值
            map.put("subject", zhi);
        }
        return data;
    }
    //
    /**
     * 销售统计报表 根据渠道来源进行分页查询
     * @param pageNum
     * @param pageSize
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TableDataInfo channelStatisticsList(Integer pageNum, Integer pageSize, LocalDate begin, LocalDate end) {
        //分页参数
        PageHelper.startPage(pageNum, pageSize);
        //日期格式转换
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        //通过mapper查询分页数据
        Page<ChannelVO> page = reportMapper.channelStatisticsList(beginTime, endTime);
        List<ChannelVO> pageResult = page.getResult();
        for (ChannelVO channelVO : pageResult) {
            channelVO.setTotal_amount(channelVO.getTotalAmount());
            String channel = channelVO.getChannel();
            //判断渠道
            if (channel != null && channel.length() > 0) {
                //通过mapper获取字典中的渠道名
                channel = sysDictDataMapper.selectChannel(channel);
                channelVO.setChannel(channel);
            } else {
                //如果渠道为空则将其渠道定义为面议直签
                channelVO.setChannel("面议直签");
            }
        }
        //封装分页查询对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(pageResult);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
        tableDataInfo.setParams(null);

        return tableDataInfo;
    }
}